﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FrmHotel
{
    public partial class FrmXvZhu : Form
    {
        public FrmXvZhu()
        {
            InitializeComponent();
        }

        public int id;//接收主菜单的房间号 
        private void FrmXvZhu_Load(object sender, EventArgs e)
        {
            Data();
        }
        public void Data()
        {
            string sql = string.Format(@"select c.CeName,c.CeSex,c.CeAge,s.CetaeName,c.CeHao,r.RmId,mm.RoomName,c.CePhone,c.CeAddress 
            from CetaeInfo c,RoomInfo r,CetaeClass s,Room mm where mm.RoomId=r.RmRoom and c.CeFangHao=r.RmId and c.CetaeClass=s.CetaeId and r.RmId={0}", id);
            SqlDataReader reader = DBHelper.SelectMore(sql);
            if (reader.Read())
            {
                this.txt_name.Text = reader["CeName"].ToString();
                if (reader["CeSex"].ToString().Equals("男"))
                {
                    this.radioButton1.Checked = true;
                }
                else 
                {
                   this.radioButton2.Checked = true;
                }
                this.txt_age.Text = reader["CeAge"].ToString();
                this.txt_zhengClass.Text = reader["CetaeName"].ToString();
                this.txt_zhengjainhao.Text = reader["CeHao"].ToString();
                this.txt_fangHao.Text = reader["RmId"].ToString();
                this.txt_FangClass.Text = reader["RoomName"].ToString();
                this.txt_Address.Text = reader["CeAddress"].ToString();
                this.txt_hoper.Text = reader["CePhone"].ToString();  
            }   
            reader.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = string.Format("select c.CeCut,f.CKXfei,m.RoomMoney from CetaeInfo c,RoomInfo r,checkInfo f,Room m where c.CeFangHao=r.RmId and f.Ckname=c.CeId and r.RmRoom=m.RoomId and r.RmId={0}", id);
            SqlDataReader reader = DBHelper.SelectMore(sql);
            string date = null;
            float fei = 0.0f;
            float money = 0.0f;
            if (reader.Read())
            {
                date = reader["CeCut"].ToString();
                fei = Convert.ToSingle(reader["CKXfei"]);
                money = Convert.ToSingle(reader["RoomMoney"]);
            }
            reader.Close();
            int Dey = Convert.ToInt32(this.numericUpDown2.Value);//续住的天数
            string sql1 = string.Format("select DateAdd(dd,{0},'{1}')",Dey,date);
            string result = DBHelper.SelectOne(sql1).ToString();

            string cha = string.Format("select DATEDIFF(DAY,'{0}','{1}')", date, result);
            int result1 = Convert.ToInt32(DBHelper.SelectOne(cha));
            float he = Convert.ToSingle(result1 * money);
            decimal Zong =Convert.ToDecimal( fei + he);

            string sql2 = string.Format("update CetaeInfo set CeCut='{0}' where CeFangHao={1}", result,id);
            int xv = DBHelper.ExecuteUpdate(sql2);

            string s = DBHelper.SelectOne(string.Format("select CeId from CetaeInfo where CeFangHao={0}", id)).ToString();
            int s1 = Convert.ToInt32(s);
            DateTime dt = DateTime.Now;

            string sql3 = string.Format("update checkInfo set CKXfei='{0}' where Ckname={1}", Zong,s1);
            int xv1 = DBHelper.ExecuteUpdate(sql3);
            if (xv>0&&xv1>0)
            {
                string ooo = string.Format("insert into Tongji values({0},'{1}')", he, dt);//统计
                int ererer = DBHelper.ExecuteUpdate(ooo);
                MessageBox.Show("该房间续住成功！","提示信息！");
            }
            else
            {
                MessageBox.Show("续住失败！","提示信息！");
                return;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
